package com.eagle.epay.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

public class MysqlGenerator {

	private MysqlGenerator() {

	}

	public static void Generator(String[] tableName) {
		String projectPath = System.getProperty("user.dir");
//		System.out.println(projectPath);
//		//============================== 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(projectPath + "/mybatisplus/src/main/java")
				.setActiveRecord(true)// 是否支持 AR
				.setAuthor("eagle") //设置作者名字
				.setFileOverride(true) //文件覆盖(全新文件)
				.setIdType(IdType.AUTO)//主键策略
				.setBaseResultMap(true) //SQL 映射文件
				.setBaseColumnList(true)//SQL 片段
				.setOpen(false);
		//============================== 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL)
				.setUrl("jdbc:mysql://192.168.56.131:3306/epay-mail?useUnicode=true&characterEncoding=utf8")
				.setDriverName("com.mysql.jdbc.Driver")
				.setUsername("root")
				//.setSchemaName("public")
				.setPassword("123456");

		//==============================包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.eagle.epay")//配置父包路径
//				.setModuleName("base")//配置业务包路径
				.setMapper("mapper")
				.setEntity("entity")
				.setService("service")
				.setController("controller")
				.setServiceImpl("service.impl"); //会自动生成 impl，可以不设定

		//============================== Mappper配置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};
		List<FileOutConfig> focList = new ArrayList<>();
		focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输入文件名称
				return projectPath + "/mybatisplus/src/main/resources/mybatis/"
						+ "/" + tableInfo.getEntityName() + "Mapper.xml";
			}
		});
		cfg.setFileOutConfigList(focList);
		//============================== 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel)//设置命名规则  underline_to_camel 底线变驼峰
				.setColumnNaming(NamingStrategy.underline_to_camel)//设置设置列命名  underline_to_camel 底线变驼峰
				//.setSuperEntityClass("com.maoxs.pojo")//设置继承类
				//.setSuperControllerClass("com.maoxs.controller")//设置继承类
				.setEntityLombokModel(true)//是否加入lombok
				.setInclude(tableName)//设置表名
				//.setSuperEntityColumns("id") //设置超级超级列
				.setControllerMappingHyphenStyle(true)//设置controller映射联字符
				.setTablePrefix("mall_");//表的前缀
		//============================== 生成配置
		AutoGenerator mpg = new AutoGenerator();
		mpg.setCfg(cfg)
				.setTemplate(new TemplateConfig().setXml(null))
				.setGlobalConfig(gc)
				.setDataSource(dsc)
				.setPackageInfo(pc)
				.setStrategy(strategy);
//				// 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
//				.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}

	public static void main(String[] args) {
//		Generator(new String[]{
//				"mall_category","mall_order","mall_order_item","mall_pay_info","mall_product","mall_shipping"});
		Generator(new String[]{"monitor"});
	}
}